/***********************************************************
 * Product Name: ExcelQuicker
 * Copyright (c)2005, Eunge
 * Author: Eunge
 * Web Site: www.eudev.net
 * Mail/MSN: eudev.net@yeah.net
 * ********************************************************/
using System;

namespace ExcelQuicker.Framework
{
	/// <summary>
	/// This class stands for the operation on a cell of excel document. You can set its message, color, border and so on
	/// </summary>
	/// <example>
	/// <br>EQCell cell = new EQCell("The title of report", 1, 1)</br>
	/// <br>//We assume that we have defined objEQRangeStyle before.</br>
	/// <br>cell.AddStyle(objEQRangeStyle);</br>
	/// <br>objEQWorksheet.Add(cell);</br>
	/// </example>
	[Serializable]
	public class EQCell: EQTextBase  
	{
		private int mTop = DefaultInt;
		private int mLeft = DefaultInt;
		private EQType mType = EQType.Text;
		private object mText;
		private EQEventRangeHandler mAfterOperationEvent;
		private EQEventRangeHandler mBeforeOperationEvent;

		/// <summary>
		/// Set/Get the event before operation of this object
		/// </summary>
		public new EQEventRangeHandler BeforeOperationEvent
		{
			get
			{
				return this.mBeforeOperationEvent;
			}
			set
			{
				this.mBeforeOperationEvent = value;
			}
		}

		/// <summary>
		/// Set/Get the event after operation of this object
		/// </summary>
		public new EQEventRangeHandler AfterOperationEvent
		{
			get
			{
				return this.mAfterOperationEvent;
			}
			set
			{
				this.mAfterOperationEvent = value;
			}
		}

		/// <summary>
		/// Get column index of the cell
		/// </summary>
		public int Left
		{
			get
			{
				return this.mLeft;
			}
		}

		/// <summary>
		/// Set column index of the cell
		/// </summary>
		/// <param name="v"></param>
		internal void SetLeft(int v)
		{
			this.mLeft = EQKit.CheckPosition(v);
		}

		/// <summary>
		/// Get row index of the cell
		/// </summary>
		public int Top
		{
			get
			{
				return this.mTop;
			}
		}

		/// <summary>
		/// Set row index of the cell
		/// </summary>
		/// <param name="v"></param>
		internal void SetTop(int v)
		{
			this.mTop = EQKit.CheckPosition(v);
		}

		/// <summary>
		/// Set/Get whether the type of this cell is text. It also means whether the type of this cell is EQType.Text
		/// </summary>
		public bool IsText
		{
			get
			{
				return (this.mType==EQType.General)?false:true;
			}
			set
			{
				this.mType = value?EQType.Text:EQType.General;
			}
		}

		/// <summary>
		/// Get text of current cell
		/// </summary>
		public object Text
		{
			get
			{
				return this.mText;
			}
		}

		/// <summary>
		/// Set/Get the type of cell's message. Default value is EQType.Text
		/// </summary>
		public EQType Type
		{
			get
			{
				return this.mType;
			}
			set
			{
				this.mType = value;
			}
		}


		/// <summary>
		/// Output this cell to excel document by Excel.Worksheet COM Interface
		/// </summary>
		/// <param name="objWorksheet">Excel.Worksheet</param>
		protected internal override void Output(Excel.Worksheet objWorksheet)
		{
			if(this.mTop == DefaultInt || this.mLeft == DefaultInt)
				throw new EQException("EQ1001", this.GetType().ToString());
			Excel.Range objRange = (Excel.Range)objWorksheet.Cells[this.Top, this.Left];
			this.Output(objRange);
		}

		/// <summary>
		/// Output this cell to excel document by Excel.Range COM Interface
		/// </summary>
		/// <param name="objRange">Excel.Range</param>
		protected internal void Output(Excel.Range objRange)
		{
			if(this.BeforeOperationEvent != null)
				this.BeforeOperationEvent(objRange);	//Raised before operation event

			if(this.Type == EQType.Text)
				objRange.NumberFormatLocal = "@";

			objRange.Value = this.Text;

			this.Style.Output(objRange);

			if(this.AfterOperationEvent != null)	//Raised after operation event
				this.AfterOperationEvent(objRange);
		}


		/// <summary>
		/// Initialize object
		/// </summary>
		/// <param name="text">The message of the cell, object only can be primitive type(Like int, decimal, doble...), string and DateTime</param>
		public EQCell(object text)
		{
			this.mText = text;
		}


		/// <summary>
		/// Initialize object
		/// </summary>
		/// <param name="text">The message of the cell, object only can be primitive type(Like int, decimal, doble...), string and DateTime</param>
		/// <param name="isText">Whether the type of message is text</param>
		public EQCell(object text, bool isText)
		{
			this.mText = text;
			this.IsText = isText;
		}

		/// <summary>
		/// Initialize object
		/// </summary>
		/// <param name="text">The message of the cell, object only can be primitive type(Like int, decimal, doble...), string and DateTime</param>
		/// <param name="top"></param>
		/// <param name="left"></param>
		/// <param name="isText">Whether the type of message is text</param>
		public EQCell(object text, int top, int left, bool isText)
		{
			this.mText = text;
			this.SetTop(top);
			this.SetLeft(left);
			this.IsText = isText;
		}

		/// <summary>
		/// Initialize object
		/// </summary>
		/// <param name="text">The message of the cell, object only can be primitive type(Like int, decimal, doble...), string and DateTime</param>
		/// <param name="top">Row index</param>
		/// <param name="left">Column Index</param>
		public EQCell(object text, int top, int left)
		{
			this.mText = text;
			this.SetTop(top);
			this.SetLeft(left);
			this.IsText = true;
		}

		/// <summary>
		/// Raised after the operation of this object
		/// </summary>
		/// <param name="objRange"></param>
		protected void DoAfterOperationEvent(Excel.Range objRange)
		{
			if(this.AfterOperationEvent != null)
				this.AfterOperationEvent(objRange);
		}

		/// <summary>
		/// Raised before the operation of this object
		/// </summary>
		/// <param name="objRange"></param>
		protected void DoBeforeOperationEvent(Excel.Range objRange)
		{
			if(this.BeforeOperationEvent != null)
				this.BeforeOperationEvent(objRange);
		}
	}
}
